/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import frm.billingAndPaymentProcessing.entity.InvoiceEntity;
import frm.billingAndPaymentProcessing.entity.ReceiptEntity;
import frm.billingAndPaymentProcessing.session.InvoiceSessionBean;
import frm.billingAndPaymentProcessing.session.ReceiptSessionBean;
import java.io.IOException;
import java.util.Calendar;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class CreateReceiptManagedBean {
    private Long invoiceId; 
    private ReceiptEntity receipt;
    
    @EJB
    ReceiptSessionBean receiptSessionBean;
    
    @EJB
    InvoiceSessionBean invoiceSessionBean;
    /** Creates a new instance of CreateReceiptManagedBean */
    public CreateReceiptManagedBean() {
       receipt = new ReceiptEntity();
    }
    
    public void createReceipt() throws IOException{
         HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long employeeId = (Long) request.getSession().getAttribute("userId");
        InvoiceEntity invoice = invoiceSessionBean.getInvoiceById(invoiceId);
        if(invoice.getPayment_balance() >= 0l){
//            receipt.setInvoice(invoice);
            receipt.setIssuingDate(Calendar.getInstance().getTime());
            receipt.setCreatedBy(employeeId);
            invoice.setReceipt(receipt);
            //send receipt to customer
          FacesContext.getCurrentInstance().getExternalContext().getFlash().put("receipt", receipt);
          FacesContext.getCurrentInstance().getExternalContext().redirect("displayReceipt.xhtml");
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Customer's Payment Balance is not Cleaned, Pls Check", ""));   
    }
    public ReceiptEntity getReceipt() {
        return receipt;
    }

    public void setReceipt(ReceiptEntity receipt) {
        this.receipt = receipt;
    }

    public Long getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Long invoiceId) {
        this.invoiceId = invoiceId;
    }
}
